using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Data;

using PlugNT.Safe;
using PlugNT.Database;
using PlugNT.Database.Common.Simple;
using PlugNT.Cms.Custom;
using PlugNT.Cms.BLL;
using PlugNT.App.Shop.Model;

namespace PlugNT.App.Shop.BLL
{
    public class Order
    {

        private static readonly PlugNT.App.Shop.DAL.Order dal = new PlugNT.App.Shop.DAL.Order();








        public bool Create(OrderInfo model)
        {
            return dal.Create(model);
        }










        public bool EntryRefund(int id, int userId, out string error)
        {
            return dal.EntryRefund(id,userId,out error);
        }








        public bool SendGoods(int id, out string error)
        {
            return dal.SendGoods(id, out error);
        }
















        public DataTable GetList(int sysModelId, int pageSize, int currPage, int pState, int gState, int eState, int oState, int sOnline,
             string orderNo, out int recordCount)
        {

            StringBuilder andWhere = new StringBuilder();


            if (pState != -1)
            {

                andWhere.Append(" and [pay_state]="+ pState.ToString());
            }

            if (gState != -1)
            {
                andWhere.Append(" and [state_goods]=" + gState.ToString());
            }

            if (eState != -1)
            {
                andWhere.Append(" and [state_entry]=" + eState.ToString());
            }

            if (oState != -1)
            {
                andWhere.Append(" and [state]=" + oState.ToString());
            }

            if (sOnline != -1)
            {
                andWhere.Append(" and [state_online]=" + sOnline.ToString());
            }

            if (orderNo != "")
            {
                andWhere.Append(" and [no]='" + orderNo +"'");
            }

            LeftJoinInfo joinInfo = new LeftJoinInfo(WebConfig.UsersTableName, "id,username,email", "user_id", WebConfig.GetUserField("id"), "");
            recordCount = dal.GetCount(sysModelId, andWhere.ToString());

            return dal.GetList(sysModelId, pageSize, currPage,andWhere.ToString(), joinInfo);
        }
















        public int GetCount(int sysModelId, int pState, int gState, int eState, int oState, int pId, int dId, int sOnline, int eDays)
        {
            StringBuilder andWhere = new StringBuilder();


            if (pState != -1)
            {

                andWhere.Append(" and [pay_state]=" + pState.ToString());
            }

            if (gState != -1)
            {
                andWhere.Append(" and [state_goods]=" + gState.ToString());
            }

            if (eState != -1)
            {
                andWhere.Append(" and [state_entry]=" + eState.ToString());
            }

            if (oState != -1)
            {
                andWhere.Append(" and [state]=" + oState.ToString());
            }

            if (pId != -1)
            {
                andWhere.Append(" and payonline_id=" + pId.ToString());
            }

            if (dId != -1)
            {
                andWhere.Append(" and delivery_id=" + dId.ToString());
            }

            if (sOnline != -1)
            {
                andWhere.Append(" and [state_online]=" + sOnline.ToString());
            }

            if (eDays > 0)
            {
                andWhere.Append(WebConfig.IsAccess ? " and datediff('d',create_time,Now())=" : " and datediff(day,create_time,getdate())=");
                andWhere.Append(eDays.ToString());
            }

            return dal.GetCount(sysModelId, andWhere.ToString());

        }


















        public DataTable GetDownList(int topCount,int sysModelId, int pState, int gState, int eState, int oState, int pId, int dId, int sOnline, int eDays)
        {

            StringBuilder andWhere = new StringBuilder();


            if (pState != -1)
            {

                andWhere.Append(" and [pay_state]=" + pState.ToString());
            }

            if (gState != -1)
            {
                andWhere.Append(" and [state_goods]=" + gState.ToString());
            }

            if (eState != -1)
            {
                andWhere.Append(" and [state_entry]=" + eState.ToString());
            }

            if (oState != -1)
            {
                andWhere.Append(" and [state]=" + oState.ToString());
            }

            if (pId != -1)
            {
                andWhere.Append(" and payonline_id=" + pId.ToString());
            }

            if (dId != -1)
            {
                andWhere.Append(" and delivery_id=" + dId.ToString());
            }

            if (sOnline != -1)
            {
                andWhere.Append(" and [state_online]=" + sOnline.ToString());
            }



            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top "+ topCount +" a.*,b.id,b.username,b.email from " + PlugNT.App.Shop.DAL.Order.currTableName + " as a ");
            strSql.Append("left join " + PlugNT.Cms.Custom.WebConfig.UsersTableName);
            strSql.Append(" as b on a.user_id=b.id where a.sys_model_id=" + sysModelId + andWhere.Replace(" and "," and a."));

            if (eDays > 0)
            {
                strSql.Append(WebConfig.IsAccess ? " and datediff('d',a.create_time,Now())=" : " and datediff(day,a.create_time,getdate())=");
                strSql.Append(eDays.ToString());
            }
            return dal.GetList(strSql.ToString());

        }













        public DataTable GetList(int sysModelId, int pageSize, int currPage, int userId,int pState, out int recordCount)
        {
           
            StringBuilder andWhere = new StringBuilder();
            if (userId > 0)
            {
                andWhere.Append(" and [user_id]=" + userId.ToString());
            }
            if (pState > 0)
            {
                andWhere.Append(" and [pay_state]=" + pState.ToString());
            }
            recordCount = dal.GetCount(sysModelId, andWhere.ToString());

            LeftJoinInfo joinInfo = new LeftJoinInfo(WebConfig.UsersTableName, "id,username,email", "user_id", WebConfig.GetUserField("id"), "");
            return dal.GetList(sysModelId, pageSize, currPage, andWhere.ToString(), joinInfo);
        }



        public bool Exists(int sysModelId, int userId)
        {
            return dal.Exists(sysModelId, userId);
        }



        public OrderInfo GetOrderInfo(int id)
        {
            return PlugNT.App.Shop.DAL.Order.GetOrderInfo(id);
        }


        public OrderInfo GetOrderInfo(string no)
        {
            return PlugNT.App.Shop.DAL.Order.GetOrderInfo(no);
        }






    }
}




